package com.lhf66.controller;

import com.lhf66.domain.Meta;
import com.lhf66.domain.School;
import com.lhf66.service.SchoolService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/school")
public class SchoolController {

    @Autowired
    private SchoolService schoolService;

    @Autowired
    public Meta meta;

    @RequestMapping("/schooldata")
    @ResponseBody
    public Object schoolList(@RequestParam(value = "schoolId",required = false,defaultValue = "0") String schId) throws IOException {
        System.out.println("###############\n"+schId);
        List<School> schoolDataList = schoolService.getSchoolDataList(Integer.parseInt(schId));
        System.out.println("###############\n"+schoolDataList);


        JSONObject jsonObject = new JSONObject();

        // 讲对象数组转换成json数组
        JSONArray jsonArray = JSONArray.fromObject(schoolDataList);  // 获取json数组
        jsonObject.put("message", jsonArray);

        meta.setMsg("获取成功");
        meta.setStatus(200);
        jsonObject.put("meta",meta);

        System.out.println("###############\n"+jsonObject);

        return jsonObject;
    }

}
